import type { Component } from 'vue'
import { defineComponent, h } from 'vue'
import { AutoComplete, Cascader, Checkbox, DatePicker, Divider, Input, InputNumber, Radio, Rate, Select, Slider, Switch, TimePicker, TreeSelect, Transfer } from 'ant-design-vue'
import ApiCascader from './components/ApiCascader.vue'
import ApiRadioGroup from './components/ApiRadioGroup.vue'
import ApiSelect from './components/ApiSelect.vue'
import ApiTransfer from './components/ApiTransfer.vue'
import ApiTree from './components/ApiTree.vue'
import ApiTreeSelect from './components/ApiTreeSelect.vue'
// 将 comp 包装成默认 props 的组件（用户传入优先）
function withDefaults(comp: any, defaults: Record<string, any>) {
  return defineComponent({
    name: `WithDefaults(${comp.name || 'Comp'})`,
    inheritAttrs: false,
    setup(_, { attrs, slots }) {
      return () => {
        const props = { ...defaults, ...(attrs || {}) }
        return h(comp, props, slots)
      }
    }
  })
}
const componentMap = new Map()

componentMap.set('Input', withDefaults(Input, { allowClear: true }))
componentMap.set('InputGroup', Input.Group)
componentMap.set('InputPassword', withDefaults(Input.Password, { allowClear: true }))
componentMap.set('InputSearch', withDefaults(Input.Search, { allowClear: true }))
componentMap.set('InputTextArea', withDefaults(Input.TextArea, { allowClear: true }))
componentMap.set('InputNumber', InputNumber)
componentMap.set('AutoComplete', withDefaults(AutoComplete, { allowClear: true }))
componentMap.set('Select', withDefaults(Select, { allowClear: true }))
componentMap.set('ApiSelect', ApiSelect)
componentMap.set('ApiTree', ApiTree)
componentMap.set('TreeSelect', withDefaults(TreeSelect, { allowClear: true }))
componentMap.set('ApiTreeSelect', ApiTreeSelect)
componentMap.set('ApiRadioGroup', ApiRadioGroup)
componentMap.set('Switch', Switch)
componentMap.set('RadioGroup', Radio.Group)
componentMap.set('Checkbox', Checkbox)
componentMap.set('CheckboxGroup', Checkbox.Group)
componentMap.set('ApiCascader', ApiCascader)
componentMap.set('Cascader', Cascader)
componentMap.set('Slider', Slider)
componentMap.set('Rate', Rate)
componentMap.set('Transfer', Transfer)
componentMap.set('ApiTransfer', ApiTransfer)

componentMap.set('DatePicker', DatePicker)
componentMap.set('MonthPicker', DatePicker.MonthPicker)
componentMap.set('RangePicker', DatePicker.RangePicker)
componentMap.set('WeekPicker', DatePicker.WeekPicker)
componentMap.set('TimePicker', TimePicker)
componentMap.set('TimeRangePicker', TimePicker.TimeRangePicker)

componentMap.set('Divider', Divider)

export const add = <T extends string, R extends Component>(compName: T, component: R) => {
  componentMap.set(compName, component)
}

export const del = <T extends string>(compName: T) => {
  componentMap.delete(compName)
}

export { componentMap }

export type ComponentName =
  | 'Input'
  | 'InputGroup'
  | 'InputPassword'
  | 'InputSearch'
  | 'InputTextArea'
  | 'InputNumber'
  | 'AutoComplete'
  | 'Select'
  | 'ApiSelect'
  | 'ApiTree'
  | 'TreeSelect'
  | 'ApiTreeSelect'
  | 'ApiRadioGroup'
  | 'Switch'
  | 'RadioGroup'
  | 'Checkbox'
  | 'CheckboxGroup'
  | 'ApiCascader'
  | 'Cascader'
  | 'Slider'
  | 'Rate'
  | 'Transfer'
  | 'ApiTransfer'
  | 'DatePicker'
  | 'MonthPicker'
  | 'RangePicker'
  | 'WeekPicker'
  | 'TimePicker'
  | 'TimeRangePicker'
  | 'Divider'
  | Component
